#!/usr/bin/python
# -*- coding: utf-8 -*-
# Description: unittest for spider.py
# Create at 2008-11-1 下午01:19:20
# Author: MK2[fengmk2@gmail.com]
"""Unit Test for spider.Spider
"""

import unittest
import spider

class SpiderTest(unittest.TestCase):
    """Spider UnitTest Case"""
    
    baidu_templates = [
        {
         'next_level': ur"""
             <a[^<>]*?href="(\d+/\d+_simp3\.htm)"[^<>]*?>[^<>]*?</a>
            """,
        },
        {
         'infos': ur"""
            <td[^<>]*?><a[^<>]*?href="(?P<nexturl>[^<>]*)"[^<>]*?>[\s\S]*?</a></td>
            [\s\S]*?
<td[^<>]*?><a[^<>]*?>(?P<author>[\s\S]*?)</a>[\s\S]*?</td>
[\s\S]*?
<td[^<>]*?><a[^<>]*?>(?P<album>[\s\S]*?)</td>
[\s\S]*?
<td[^<>]*?>[\s\S]*?</td>[\s\S]*?
<td[^<>]*?>[\s\S]*?</td>[\s\S]*?
<td[^<>]*?>[\s\S]*?</td>[\s\S]*?
<td[^<>]*?>(?P<size>[^<>]*)</td>[\s\S]*?
<td[^<>]*?>[^<>]*?</td>[\s\S]*?
<td[^<>]*?><img[^<>]*?src="[^<>]*?d(?P<speed>\d+).gif"[^<>]*?></td>
            """
         },
#        {'downurl': ur"""
#            <img[^<>]*?src="/skin/xdowns/icon_downloadserver.gif"[^<>]*?>[^<>]*?
#            <a[^<>]*?href="([^<>]*?)"[^<>]*?>[^<>]*?</a>
#        """},
    ]
    
    def setUp(self):
        "Hook method for setting up the test fixture before exercising it."
        self.baidu_spider = spider.Spider('http://list.mp3.baidu.com/singer/singers.html', 
                                          self.baidu_templates)
    def test_baidu(self):
        for level, url, parenturl, response, \
                    content, downurls, softinfo in self.baidu_spider.walk():
            print '-' * 60
            if softinfo:
                print level, url, parenturl, len(downurls)
                for k, v in softinfo.iteritems():
                    print k, ':', v
            else:
                print level, url, parenturl, downurls

if __name__ == '__main__':
    unittest.main()